//
//  QTIndexViewController.m
//  qdqts
//
//  Created by jonn on 14-5-27.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTIndexViewController.h"
#import "MainViewController.h"
#import "QTHistoryProductTableViewController.h"
#import "CodaBarScanViewController.h"
#import "QTLoginViewController.h"
#import "QTUserDefault.h"
#import "QTUserInfoViewController.h"
#import "QTAboutViewController.h"
#import "Global.h"

@interface QTIndexViewController ()

@end

@implementation QTIndexViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate=self;
    self.viewControllers = [NSArray arrayWithObjects:
                            [self viewControllerWithTabTitle:@"首页" image:[UIImage imageNamed:@"tabBar_home.png"] index:0],
                            [self viewControllerWithTabTitle:@"历史记录" image:[UIImage imageNamed:@"tabBar_history.png"]index:1],
                            [self viewControllerWithTabTitle:@"" image:nil index:0],
                            [self viewControllerWithTabTitle:@"关于我们" image:[UIImage imageNamed:@"tabBar_info.png"] index:3],
                            [self viewControllerWithTabTitle:@"个人中心" image:[UIImage imageNamed:@"tabBar_user.png"] index:4],NULL];
    [self addCenterButtonWithImage:[UIImage imageNamed:@"scan_nor.png"] highlightImage:[UIImage imageNamed:@"scan_sel.png"]];
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIViewController *)getViewcontroller:(int)index
{

    switch (index) {
        case 0:{
            MainViewController *mainPageViewController=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
            mainPageViewController.hidenTabBarDelegate=self;
            UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:mainPageViewController];
            return navCtrl;
            break;
        }
        case 1:{
            QTHistoryProductTableViewController *historyProduct=[[QTHistoryProductTableViewController alloc] initWithNibName:@"QTHistoryProductTableViewController" bundle:nil];
            historyProduct.hidenTabBarDelegate=self;
            UINavigationController *navCtrl1 = [[UINavigationController alloc] initWithRootViewController:historyProduct];
            return navCtrl1;
            break;
        }
        case 2:{
            return nil;
            break;
        }
        case 3:{
            QTAboutViewController *mainPageViewController3=[[QTAboutViewController alloc] initWithNibName:@"QTAboutViewController" bundle:nil];
            UINavigationController *navCtrl3 = [[UINavigationController alloc] initWithRootViewController:mainPageViewController3];
            return navCtrl3;
            break;
        }
        case 4:{
            QTLoginViewController *loginViewController=[[QTLoginViewController alloc] initWithNibName:@"QTLoginViewController" bundle:nil];
            
            QTUserDefault *userDefault=[QTUserDefault sharedInstance];
            //    NSString *userName=[userDefault readNSUserDefaults:@"userName"];
            //    NSString *passwd=[userDefault readNSUserDefaults:@"passwd"];
            NSString *puid=[userDefault readNSUserDefaults:@"puid"];
            UINavigationController *navCtrl4 = [[UINavigationController alloc] init];
            
            if(!([puid isEqualToString:@""]||[puid isEqualToString:nil]||(puid==nil))){
                //here
                QTUserInfoViewController *userInfoController = [[QTUserInfoViewController alloc] init];
                [navCtrl4 pushViewController:userInfoController animated:YES];
            }else {
                [navCtrl4 pushViewController:loginViewController animated:YES];
            }
            return navCtrl4;
            break;
        }
        default:
            break;
    }
    
//    NSArray* tabBarItems = nil;
//    MainViewController *mainPageViewController=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
//    mainPageViewController.hidenTabBarDelegate=self;
//    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:mainPageViewController];
////    QTHistoryViewController *mainPageViewController1=[[QTHistoryViewController alloc] initWithNibName:@"QTHistoryViewController" bundle:nil];
////    UINavigationController *navCtrl1 = [[UINavigationController alloc] initWithRootViewController:mainPageViewController1];
//    QTHistoryProductTableViewController *HistoryProduct=[[QTHistoryProductTableViewController alloc] initWithNibName:@"QTHistoryProductTableViewController" bundle:nil];
//    UINavigationController *navCtrl1 = [[UINavigationController alloc] initWithRootViewController:HistoryProduct];
//
//    UIViewController *viewController=[[UIViewController alloc] init];
//    
//    QTAboutViewController *mainPageViewController3=[[QTAboutViewController alloc] initWithNibName:@"QTAboutViewController" bundle:nil];
//    UINavigationController *navCtrl3 = [[UINavigationController alloc] initWithRootViewController:mainPageViewController3];
//    
//    QTLoginViewController *loginViewController=[[QTLoginViewController alloc] initWithNibName:@"QTLoginViewController" bundle:nil];
//    
//    QTUserDefault *userDefault=[QTUserDefault sharedInstance];
////    NSString *userName=[userDefault readNSUserDefaults:@"userName"];
////    NSString *passwd=[userDefault readNSUserDefaults:@"passwd"];
//    NSString *puid=[userDefault readNSUserDefaults:@"puid"];
//    UINavigationController *navCtrl4 = [[UINavigationController alloc] init];
//    
//    if(!([puid isEqualToString:@""]||[puid isEqualToString:nil]||(puid==nil))){
//        //here
//        QTUserInfoViewController *userInfoController = [[QTUserInfoViewController alloc] init];
//        [navCtrl4 pushViewController:userInfoController animated:YES];
//    }else {
//        [navCtrl4 pushViewController:loginViewController animated:YES];
//    }
//    tabBarItems = [NSArray arrayWithObjects:navCtrl,navCtrl1,viewController,navCtrl3,navCtrl4,nil];
//    
//    return (UIViewController *)[tabBarItems objectAtIndex:index];
    
    return nil;
}


// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image index:(int)index
{
    
    UIViewController* viewController = [self getViewcontroller:index];
    viewController.tabBarItem=[[UITabBarItem alloc] initWithTitle:title image:image tag:index];
    
    return viewController;
};

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button addTarget:self action:@selector(scanClick) forControlEvents:UIControlEventTouchUpInside];
    _button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    _button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [_button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [_button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        _button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        _button.center = center;
    }
    
    [self.view addSubview:_button];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(void)scanClick
{
    //进入二维码扫描界面
    CodaBarScanViewController *codaBarScanViewController = [[CodaBarScanViewController alloc] initWithNibName:@"CodaBarScanViewController" bundle:nil];
    codaBarScanViewController.title = @"扫码";
    UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:codaBarScanViewController];
    [navCtrl.navigationBar setHidden:YES];
    [self presentViewController:navCtrl animated:YES completion:nil];
}

-(void)hideTabBar:(BOOL)status
{
    self.tabBar.hidden=status;
    self.button.hidden=status;
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

    if(selectedViewController==viewController){
        NSLog(@"THE same");
        return 0;
    }
    else{
        selectedViewController=viewController;
        NSLog(@"Different");
        return 1;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
